//## 단위 작업 정보 저장 ####################
function fnSave()
{
	if (!$("#processNm").val())
	{
		alert("단위작업명을 바르게 입력해 주세요!");
		$("#processNm").focus();
		return;
	}
	var param  = "processNm="+encodeURIComponent($("#processNm").val());
		param += "&working="+encodeURIComponent($("#working").val());
		param += "&execTime="+encodeURIComponent($("#execTime").val());
		param += "&type="+$("#type").val();
		param += "&processCd="+$("#processCd").val();
		param += "&flag="+$("#flag").val();

	var valUrl = '/action/unitProcess/save.asp';
	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);


	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				grid01List();
				fnReset();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 단위 작업 정보 그리드 표시 ####################

var G_grid01Rslt = "" //XML Data를 받을 전역변수 선언

function grid01List()
{
	clstTable("Grid01");
	$("#tr01Result").show();
	setNullRow("loadSpan","1");
	var valUrl = "/action/unitProcess/list.asp";
	var param = "findProcess="+encodeURIComponent($("#findProcess").val());

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			G_grid01Rslt = xml;
			setGrid01(1);
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function setGrid01(curPage)
{
	clstTable("Grid01");

	$("#tr01Result").css("display","none");
	$("#loadSpan1").html("");
	var nodeCnt = $(G_grid01Rslt).find("Process").find("ProcList").length;

	if(nodeCnt == 0)
	{
		$("#tr01Result").css("display","");
		$("#loadSpan1").html("검색된 자료가 없습니다.");
		$("#grid01Cnt").html("0");
		return;
	}
	var listNum = 50;
	var totalCount = nodeCnt;
	$("#grid01Cnt").html(totalCount);
	if (curPage == "") curPage = 1;
	var totalPage = Math.ceil(nodeCnt/listNum);
	var restArticle = (nodeCnt%listNum);
	startNo = (parseInt(curPage)-1) * listNum;	
	var endNo = startNo + listNum;
	
	nodeCnt = nodeCnt - ((curPage-1)*listNum);
	if (totalPage == curPage)
	{
		if (restArticle == 0) var endNo = startNo + listNum;
		else var endNo = startNo + restArticle;
	}

	var pages = fnPaging("Grid01", curPage, totalPage);

	$("#pageDiv01").html(pages);
	
	var grid01Array = new Array();
	var chkCnt = 0;
	for(var j=startNo; j<endNo; j++)
	{
		var processCd	= $(G_grid01Rslt).find("Process").find("ProcList").eq(j).find("processCd").text();
		var processNm	= $(G_grid01Rslt).find("Process").find("ProcList").eq(j).find("processNm").text();
		var working		= $(G_grid01Rslt).find("Process").find("ProcList").eq(j).find("working").text();
		var execTime	= $(G_grid01Rslt).find("Process").find("ProcList").eq(j).find("execTime").text();	
		var type		= $(G_grid01Rslt).find("Process").find("ProcList").eq(j).find("type").text();
		var flag		= $(G_grid01Rslt).find("Process").find("ProcList").eq(j).find("flag").text();
		var itemNm		= $(G_grid01Rslt).find("Process").find("ProcList").eq(j).find("itemNm").text();

		if (!execTime) execTime = "&nbsp;";
		if (!working) working = "&nbsp;";
		var vType = itemNm;
//		if (type == "10") vType = "가공";
//		if (type == "20") vType = "검사";
//		if (type == "30") vType = "일반";
//		if (type == "80") vType = "포장(외주)";
//		if (type == "90") vType = "포장";
//		if (type == "98") vType = "출하";

		ji = j+1;
		
		var showFlag = "";
		if(flag == "Y") showFlag = "사용";
		else showFlag = "사용안함";
		
		if (flag == "N") var bgColor = "#F2F2F2";
		else var bgColor = "#FFFFFF";

		grid01Array[0] = "<input type=\"checkbox\" name=\"procCd\" value=\""+processCd+"^"+processNm+"\" onclick=\"fnAddProcSet(this.value)\" style=\"border:0\">"+ji;
		grid01Array[1] = processNm;
		grid01Array[2] = working;
		grid01Array[3] = execTime;
		grid01Array[4] = vType;
		grid01Array[5] = showFlag;

		var boldLine = ""
		if (ji%10 == 0 && ji != 1) boldLine = "Y"
		
		mkTable2("Grid01", grid01Array, bgColor, ",,,,", "fnSetUnitProcess('"+processCd+"')", boldLine);
	}
}


//## 단위 작업 상세정보 표시 ####################
function fnSetUnitProcess(processCd)
{
	var valUrl = "/action/unitProcess/view.asp";
	var param = "processCd="+processCd;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			
			$("#processNm").val($(xml).find("processNm").text());
			$("#processCd").val($(xml).find("processCd").text());
			$("#working").val($(xml).find("working").text());
			$("#execTime").val($(xml).find("execTime").text());
			$("#type").val($(xml).find("type").text());
			$("#flag").val($(xml).find("flag").text());

		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 공정 그룹에 순서를 정하여 추가 하기
function fnAddProcSet(process)
{
	var divProc = process.split("^");
	if ($("#processSet").val().indexOf(divProc[0]+",") == -1)
	{
		$("#showProcess").append("[" + divProc[1] + "]" + " -> ");
		$("#processSet").val($("#processSet").val()+divProc[0]+",");
	}
	else
	{
		$("#showProcess").text($("#showProcess").text().replace("[" + divProc[1] + "]" + " -> ",""));
		$("#processSet").val($("#processSet").val().replace(divProc[0]+",",""));

	}
}

//## 공정 저장하기
function fnProcSetSave()
{
	if (!$("#procTypeNm").val())
	{
		alert("공정명을 바르게 입력해 주세요!");
		$("#procTypeNm").focus();
		return;
	}
	var param  = "procTypeNm="+encodeURIComponent($("#procTypeNm").val());
		param += "&procTypeCd="+$("#procTypeCd").val();
		param += "&processCds="+encodeURIComponent($("#processSet").val());
		param += "&flag="+$("#procSetFlag").val();

	var valUrl = '/action/unitProcess/saveProcSet.asp';
	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);


	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				grid02List();
				fnProcSetReset();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 공정 정보 그리드 표시 ####################

var G_grid02Rslt = "" //XML Data를 받을 전역변수 선언

function grid02List()
{
	clstTable("Grid02");
	$("#tr02Result").show();
	setNullRow("loadSpan","2");
	var valUrl = "/action/unitProcess/listProcSet.asp";
	var param = "";

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			G_grid02Rslt = xml;
			setGrid02(1);
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function setGrid02(curPage)
{
	clstTable("Grid02");

	$("#tr02Result").css("display","none");
	$("#loadSpan2").html("");
	var nodeCnt = $(G_grid02Rslt).find("ProcSet").find("ProcList").length;

	if(nodeCnt == 0)
	{
		$("#tr02Result").css("display","");
		$("#loadSpan2").html("검색된 자료가 없습니다.");
		$("#grid02Cnt").html("0");
		return;
	}
	var listNum = 50;
	var totalCount = nodeCnt;
	$("#grid02Cnt").html(totalCount);
	if (curPage == "") curPage = 1;
	var totalPage = Math.ceil(nodeCnt/listNum);
	var restArticle = (nodeCnt%listNum);
	startNo = (parseInt(curPage)-1) * listNum;	
	var endNo = startNo + listNum;
	
	nodeCnt = nodeCnt - ((curPage-1)*listNum);
	if (totalPage == curPage)
	{
		if (restArticle == 0) var endNo = startNo + listNum;
		else var endNo = startNo + restArticle;
	}

	var pages = fnPaging("Grid02", curPage, totalPage);

	$("#pageDiv02").html(pages);
	
	var grid02Array = new Array();
	var chkCnt = 0;
	for(var j=startNo; j<endNo; j++)
	{
		var procTypeCd	= $(G_grid02Rslt).find("ProcSet").find("ProcList").eq(j).find("procTypeCd").text();
		var procTypeNm	= $(G_grid02Rslt).find("ProcSet").find("ProcList").eq(j).find("procTypeNm").text();
		var procSet		= $(G_grid02Rslt).find("ProcSet").find("ProcList").eq(j).find("procSet").text();
		var issueDt		= $(G_grid02Rslt).find("ProcSet").find("ProcList").eq(j).find("issueDt").text();	
		var flag		= $(G_grid02Rslt).find("ProcSet").find("ProcList").eq(j).find("flag").text();

		if (!procSet) procSet = "&nbsp;";

		ji = j+1;
		
		var showFlag = "";
		if(flag == "Y") showFlag = "사용";
		else showFlag = "사용안함";
		
		if (flag == "N") var bgColor = "#F2F2F2";
		else var bgColor = "#FFFFFF";

		grid02Array[0] = ji;
		grid02Array[1] = procTypeNm;
		grid02Array[2] = procSet;
		//grid01Array[3] = execTime;
		grid02Array[3] = showFlag;

		var boldLine = ""
		if (ji%10 == 0 && ji != 1) boldLine = "Y"
		
		mkTable2("Grid02", grid02Array, bgColor, ",,left,,", "fnSetProcSet('"+procTypeCd+"')", boldLine);
	}
}


//## 공정 상세정보 표시 ####################
function fnSetProcSet(procTypeCd)
{
	var valUrl = "/action/unitProcess/viewProcSet.asp";
	var param = "procTypeCd="+procTypeCd;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);
	fnProcSetReset();
	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			$("#procTypeCd").val($(xml).find("procTypeCd").text());
			$("#procTypeNm").val($(xml).find("procTypeNm").text());
			$("#showProcess").html($(xml).find("procSet").text());
			$("#procSetFlag").val($(xml).find("flag").text());

		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}


//## 공정 그룹 Reset 기능
function fnProcSetReset()
{
	$("#showProcess").html("");
	$("#processSet").val("");
	$("#procTypeCd").val("");
	$("#procSetFlag").val("Y");
	$("#procTypeNm").val("");

	$("input:checkbox[name=procCd]").each(function() {
			this.checked = false;
	});
}

//## 단위 작업 정보 Reset 기능
function fnReset()
{
	$("#processNm").val("");
	$("#processCd").val("");
	$("#working").val("");
	$("#execTime").val("");
	$("#type").val("30");
	$("#flag").val("Y");
}

//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findProcess").val("");
}
